//
//  ViewController.m
//  searchController
//
//  Created by mac on 16/10/25.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "TableViewController.h"
#import "NSArray+Filter.h"
@interface ViewController () <UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating>

@property (strong,nonatomic) UITableView *tableview;
@property (strong,nonatomic) UISearchController *searchCont;

@property (strong,nonatomic) NSMutableArray *datalist;
@property (strong,nonatomic) NSMutableArray *searchlist;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];
   
    self.title = @"UI";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBarTintColor:[UIColor darkGrayColor]];
    
    
    
    
    self.searchCont = [[UISearchController alloc] initWithSearchResultsController:[[TableViewController alloc] init]];
    
    
    self.searchCont.delegate = self;
    self.searchCont.searchResultsUpdater = self;
    self.searchCont.hidesNavigationBarDuringPresentation = NO;
    //self.searchCont.searchBar.searchBarStyle = UISearchBarStyleProminent;
    //self.searchCont.searchBar.barTintColor = [UIColor blueColor];
    self.searchCont.searchBar.placeholder = @"搜索";
    
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    vi.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:vi];
    
    [vi addSubview:self.searchCont.searchBar];
}

- (void)loadData
{
    
    self.datalist = [NSMutableArray array];
    //self.datalist=[NSMutableArray arrayWithCapacity:100];
    self.searchlist = [NSMutableArray array];
    for (NSInteger i=1; i<=100; i++) {
        [self.datalist addObject:[NSString stringWithFormat:@"%ld%@",i,[self mixAlphabet]]];
    }

}

- (NSString *)mixAlphabet
{
    NSMutableArray * mixAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]];
    
    NSString *str = [[NSString alloc] init];
    for(int i=0; i<3; i++) {
        int x = arc4random() % 25;
        //NSLog(@"x=%d",x);
        str = [NSString stringWithFormat:@"%@%@",str,mixAlphabet[x]];
        //NSLog(@"%@",str);
    }
    return str;
}



- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    [self.searchlist removeAllObjects];
   
    self.searchlist = (NSMutableArray *)[self.datalist filter:^BOOL(id obj) {
        if([obj containsString:self.searchCont.searchBar.text]||[obj isEqualToString:self.searchCont.searchBar.text])
        {
            return YES;
        }
        else return NO;
    }];
    
    TableViewController *table = (TableViewController *)searchController.searchResultsController;
    table.data = [NSMutableArray arrayWithArray:self.searchlist];
    table.serchStr = self.searchCont.searchBar.text;
    [table.tableView reloadData];
    
//    NSLog(@"遍历数组:");
//    for(NSString *str in self.searchlist)
//    {
//        NSLog(@"%@",str);
//    }

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       return self.datalist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
   
    cell.textLabel.text = self.datalist[indexPath.row];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
